package com.westvalley.common.kit;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.codec.binary.Hex;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class SignKit {
	/**
	 * SHA265签名
	 * @param params
	 * @param key
	 * @return
	 */
	public static String signSHA265(Map<String,String> params,String key){
		Map<String,String> map = Maps.newLinkedHashMap();
		List<String> list = Lists.newArrayList();
		for(Entry<String, String> entry : map.entrySet()){
			list.add(entry.getKey()+entry.getValue());
		}
		String[] arr = list.toArray(new String[0]); 
		Arrays.sort(arr);
		return getSHA256Str(StringKit.list2String(arr, "", false)+key);
	}
	
	public static String getSHA256Str(String str){
        MessageDigest messageDigest;
        String encdeStr = "";
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(str.getBytes("UTF-8"));
            encdeStr = Hex.encodeHexString(hash);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } 
        return encdeStr;
    }
}
